//==================================================================
/// Framework_PC.cpp
///
/// Created by Davide Pasca - 2009/10/5
/// See the file "license.txt" that comes with this project for
/// copyright info. 
//==================================================================

#include "stdafx.h"
#include <shellapi.h>
#include "Framework_PC.h"
#include "resource.h"

//==================================================================
static FWPC_EvenCBType	_g_pEventCB = 0;
static void				*_g_puserData = 0;

//==================================================================
void FWPC_OnCreate( HWND hWnd );

//==================================================================
void FWPC_SetEvenCB( FWPC_EvenCBType pEventCB, void *pUserData )
{
	_g_pEventCB		= pEventCB;
	_g_puserData	= pUserData;
}

//==================================================================
#define MAX_LOADSTRING 100

//==================================================================
// Global Variables:
static HINSTANCE	_gs_hInst;								// current instance
static TCHAR		_gs_szTitle[MAX_LOADSTRING];			// The title bar text
static TCHAR		_gs_szWindowClass[MAX_LOADSTRING];		// the main window class name

//==================================================================
// Forward declarations of functions included in this code module:
static ATOM				MyRegisterClass(HINSTANCE hInstance);
static BOOL				InitInstance(HINSTANCE, int);
static INT_PTR CALLBACK	AboutDialog(HWND, UINT, WPARAM, LPARAM);

LRESULT CALLBACK		WndProc(HWND, UINT, WPARAM, LPARAM);

//==================================================================
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
#if 0
	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF |
					_CRTDBG_DELAY_FREE_MEM_DF |
					_CRTDBG_CHECK_ALWAYS_DF |
					_CRTDBG_LEAK_CHECK_DF|
					_CRTDBG_CHECK_CRT_DF );

	_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
	//_crtBreakAlloc = 334;
#endif

	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, _gs_szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_CPPKAZRACE, _gs_szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CPPKAZRACE));

	// FWPC_OnCreate message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

//==================================================================
//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
static ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CPPKAZRACE));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_CPPKAZRACE);
	wcex.lpszClassName	= _gs_szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//==================================================================
//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   _gs_hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(
				_gs_szWindowClass,
				_gs_szTitle,
				WS_OVERLAPPEDWINDOW,
				CW_USEDEFAULT,
				0,
				CW_USEDEFAULT,
				0,
				NULL,
				NULL,
				hInstance,
				NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//==================================================================
// Message handler for about box.
static INT_PTR CALLBACK AboutDialog(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		switch ( LOWORD( wParam ) )
		{
		case IDOK:
		case IDCANCEL:
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;

		case IDC_GOTOWEBSITE:
			ShellExecute(
					hDlg,
					L"open",
					L"http://kazzuya.com/kazrace",
					NULL,
					NULL,
					SW_SHOWNORMAL );
			break;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

//==================================================================
//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	if ( _g_pEventCB )
	{
		if ( _g_pEventCB( _g_puserData, hWnd, message, wParam, lParam ) )
			return 0;
	}

	switch (message)
	{
	case WM_CREATE:
		try
		{
			FWPC_OnCreate( hWnd );
		}
		catch ( std::runtime_error &e )
		{
			std::string	str = e.what();
		
			MessageBoxA( NULL, str.c_str(), "Fatal Error", MB_OK | MB_ICONERROR );
			DestroyWindow(hWnd);
		}
		catch (...)
		{
			MessageBoxA( NULL, "Unknown exception", "Fatal Error", MB_OK | MB_ICONERROR );
			DestroyWindow(hWnd);
		}
		break;

	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(_gs_hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, AboutDialog);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;

	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		EndPaint(hWnd, &ps);
		break;

	case WM_DESTROY:
		_g_pEventCB = NULL;
		PostQuitMessage(0);
		break;

	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}
